import { displayClass } from './libvbar-m-dxdisplay.so'

const dxDisplay = {};

// Create a singleton instance of the native displayClass.
// This is executed only once when the module is first imported due to the nature of ES modules.
const displayObj = new displayClass();

/**
 * Initialize the display module.
 * * This function initializes the Capturer, G2D, and Display drivers based on the provided configuration.
 * If no options are provided, the module uses the default "Gold Standard" configuration 
 * (aligned with the native C demo).
 * * @param {Object} [options] - Optional configuration object.
 * @param {Object} [options.display] - Display driver configuration (e.g., { devpath: "/dev/dri/card0" }).
 * @param {Object} [options.rgb] - RGB layer configuration (capturer, g2d, display).
 * @param {Object} [options.nir] - NIR layer configuration (capturer, g2d, display).
 * @throws {Error} If initialization fails (e.g., device not found, resource allocation error).
 */
dxDisplay.init = function (options) {
    // Pass empty object if options is undefined/null to ensure C-side receives an object (though C handles it)
    displayObj.init(options || {});
}

/**
 * Deinitialize the display module.
 * * Stops the data processing thread and releases all hardware resources (Capturer channels, 
 * G2D channels, Display channels, and allocated buffers).
 * * @throws {Error} If deinitialization fails.
 */
dxDisplay.deinit = function () {
    displayObj.deinit();
}

/**
 * Get the current display backlight brightness.
 * @returns {number} Backlight brightness value (0-100).
 * @throws {Error} If failed to get backlight.
 */
dxDisplay.getBacklight = function () {
    return displayObj.getBacklight();
}

/**
 * Set the display backlight brightness.
 * @param {number} brightness - The brightness value to set (0-100).
 * @throws {Error} If failed to set backlight.
 */
dxDisplay.setBacklight = function (brightness) {
    if (brightness === undefined || brightness === null) {
        throw new Error("dxDisplay.setBacklight: 'brightness' parameter is required");
    }
    if (typeof brightness !== 'number' || brightness < 0 || brightness > 100) {
        throw new Error("dxDisplay.setBacklight: 'brightness' must be a number between 0 and 100");
    }
    displayObj.setBacklight(brightness);
}

/**
 * Get the screen enable status.
 * @returns {boolean} 1 if enabled, 0 if disabled.
 * @throws {Error} If failed to get enable status.
 */
dxDisplay.getEnableStatus = function () {
    return displayObj.getEnableStatus();
}

/**
 * Set the screen enable status.
 * @param {boolean|number} enable - true/1 to enable screen, false/0 to disable.
 * @throws {Error} If failed to set enable status.
 */
dxDisplay.setEnableStatus = function (enable) {
    if (enable === undefined || enable === null) {
        throw new Error("dxDisplay.setEnableStatus: 'enable' parameter is required");
    }
    displayObj.setEnableStatus(enable);
}

/**
 * Get the current display power mode.
 * @returns {number} Power mode value:
 * - 0: VBAR_DRV_DISPLAY_POWER_MODE_NORMAL (Normal mode)
 * - 1: VBAR_DRV_DISPLAY_POWER_MODE_STANDBY (Standby mode)
 * @throws {Error} If failed to get power mode.
 */
dxDisplay.getPowerMode = function () {
    return displayObj.getPowerMode();
}

/**
 * Set the display power mode.
 * @param {number} mode - The power mode to set:
 * - 0: VBAR_DRV_DISPLAY_POWER_MODE_NORMAL (Normal mode)
 * - 1: VBAR_DRV_DISPLAY_POWER_MODE_STANDBY (Standby mode)
 * @throws {Error} If failed to set power mode.
 */
dxDisplay.setPowerMode = function (mode) {
    if (mode === undefined || mode === null) {
        throw new Error("dxDisplay.setPowerMode: 'mode' parameter is required");
    }
    if (mode !== 0 && mode !== 1) {
        throw new Error("dxDisplay.setPowerMode: 'mode' must be 0 (NORMAL) or 1 (STANDBY)");
    }
    displayObj.setPowerMode(mode);
}

export default dxDisplay;